package com.example.demo01.controller;

import com.example.demo01.pojo.FormInfo;
import com.example.demo01.pojo.Record;
import com.example.demo01.pojo.User;
import com.example.demo01.pojo.UserInfo;
import com.example.demo01.service.AdminService;
import com.example.demo01.service.FormInfoService;
import com.example.demo01.service.RecordService;
import com.example.demo01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class AdminController {
   @Autowired
    private UserService userService;
   @Autowired
    private FormInfoService formInfoService;
   @Autowired
    private RecordService recordService;
   @Autowired
   private AdminService adminService;


   @RequestMapping("/admin/getAllUsers")
    public String getAllUsers(Model model){
       List<UserInfo> userInfos=adminService.getAllUserInfo();
       model.addAttribute("userInfos", userInfos);
       return "getAllUsers";
   }



   @RequestMapping("/admin/freezeUserByName/{username}")
    public String freezeUserByName(@PathVariable("username") String username){
       //修改user表中active字段
       userService.updateActiveWithFalse(username);
       return "redirect:/admin/getAllUsers";
   }

   @RequestMapping("/admin/unFreezeUserByName/{username}")
    public String unFreezeUserByName(@PathVariable("username") String username){
        userService.updateActiveWithTrue(username);
       return "redirect:/admin/getAllUsers";
   }

   @RequestMapping("/admin/addUser")
    public String addUser(String username, String password, Model model){

        //1、检查用户名是否已经存在，如果是，返回错误信息，提示用户名已经存在，请求重新输入
       User user=userService.getUserByName(username);


       boolean isUsernameExist;
       if(user==null){
           isUsernameExist=false;
       }else{
           isUsernameExist=true;
       }

       System.out.println(isUsernameExist);



       if(isUsernameExist){
            model.addAttribute("usernameExistErrorMessage","用户名已存在！") ;
           //System.out.println("usernameExistErrorMessage");
            return "addUserInput";

       }else{//2、不存在，正常生成newUser对象，并插入数据库user表

           User newUser=new User(null,username,"{noop}"+password,"是","ROLE_USER");
           userService.addUser(newUser);

           FormInfo formInfo=new FormInfo(username,null,null,null,null,"否");
           formInfoService.addFormInfo(formInfo);

           return "redirect:/admin/getAllUsers";
       }



   }

   @RequestMapping("/admin/jumpToAddUserInput")
    public String jumpToAddUserInput(){
       return "addUserInput";
   }

   @RequestMapping("/admin/getUsersUnpostToday")
    public String getUsersUnpostToday(Model model){


       List<FormInfo>  formInfos= formInfoService.getAllFormInfos();



       for (int i=0;i<formInfos.size();){
           if(formInfos.get(i).getIsPostToday().equals("是")){
               formInfos.remove(i);
               continue;
           }
           i++;
       }





       for(int i=0;i<formInfos.size();){
           String username=formInfos.get(i).getUsername();
           User user=userService.getUserByName(username);
           if(user.getActive().equals("否")){
               formInfos.remove(i);
               continue;
           }
           i++;
       }

       model.addAttribute("formInfos", formInfos);
       return "getUsersUnpostToday";
   }


   @RequestMapping("/admin/getUsersUnhealthyToday")
    public String getUsersUnhealthyToday(Model model){
       List<FormInfo> formInfos=formInfoService.getAllFormInfos();


       for(int i=0;i<formInfos.size();){
           if(formInfos.get(i).getIsPostToday().equals("否") || formInfos.get(i).getHealth().equals("良好")){
               formInfos.remove(i);
               continue;
           }
           i++;
       }

       for(int i=0;i<formInfos.size();){
           String username=formInfos.get(i).getUsername();
           User user=userService.getUserByName(username);
           if(user.getActive().equals("否")){
               formInfos.remove(i);
               continue;
           }
           i++;
       }


       model.addAttribute("formInfos", formInfos);

       return "getUsersUnhealthyToday";
   }

   @RequestMapping("/admin/jumpToGetRecordsByTime")
   public String jumpToGetRecordsByTime(){
       return "getRecordsByTime";
   }



   @RequestMapping("/admin/getRecordsByTime")
    public String getRecordsByTime(String time, Model model){

       List<Record> records=recordService.getRecordByTime(time);
       model.addAttribute("records", records);

       return "getRecordsByTime";
   }




}
